﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.controls {
    import reprise.ui.*;
    import flash.events.*;
    import reprise.events.*;
    import flash.geom.*;
    import reprise.utils.*;
    import flash.text.*;
    import flash.utils.*;

    public class Scrollbar extends UIComponent {

        protected var m_mouseWheelHitAreaX:Number;
        protected var m_mouseWheelHitAreaY:Number;
        protected var m_mouseWheelListener:Object;
        protected var m_mouseWheelHitAreaHeight:Number;
        protected var m_target:TextField;
        protected var m_scrollPosition:Number;
        protected var m_scrollUpBtn:SimpleButton;
        protected var m_scrollTrack:SimpleButton;
        protected var m_scrollThumb:SimpleButton;
        protected var m_minPos:Number;
        protected var m_textScrollOrientation:String;
        protected var m_thumbMinPos:Number;
        protected var m_mouseWheelHitAreaWidth:Number;
        protected var m_scrollIntervalID:int;
        protected var m_scrollDirection:int;
        protected var m_maxPos:Number;
        protected var m_overflowScrollMode:Boolean = true
        protected var m_thumbMaxPos:Number;
        protected var m_scrollDownBtn:SimpleButton;

        public static const ORIENTATION_HORIZONTAL:String = "horizontal";
        public static const ORIENTATION_VERTICAL:String = "vertical";
        protected static const SCROLL_DIR_DOWN:int = 1;
        protected static const SCROLL_DIR_UP:int = -1;

        public function get scrollUpBtn():SimpleButton{
            return (m_scrollUpBtn);
        }
        public function set lineScrollSize(_arg1:Number):void{
            m_currentStyles.lineScrollSize = _arg1;
            m_instanceStyles.setStyle("lineScrollSize", _arg1.toString());
        }
        override protected function validateAfterChildren():void{
            var _local3:Rectangle;
            top = top;
            left = left;
            if (((m_currentStyles.autoHide) && ((m_maxPos == m_minPos)))){
                setVisibility(false);
                super.validateAfterChildren();
                return;
            };
            var _local1:Number = ((m_currentStyles.height - m_currentStyles.marginTop) - m_currentStyles.marginBottom);
            var _local2:Number = ((_local1 - m_scrollUpBtn.outerHeight) - m_scrollDownBtn.outerHeight);
            m_scrollTrack.setStyle("height", (_local2 + "px"));
            m_scrollTrack.forceRedraw();
            super.validateAfterChildren();
            if (m_maxPos == m_minPos){
                if (m_scrollThumb.enabled){
                    m_scrollThumb.enabled = false;
                    m_scrollUpBtn.enabled = false;
                    m_scrollDownBtn.enabled = false;
                    m_scrollTrack.enabled = false;
                };
            } else {
                if (!m_scrollThumb.enabled){
                    m_scrollThumb.enabled = true;
                    m_scrollUpBtn.enabled = true;
                    m_scrollDownBtn.enabled = true;
                    m_scrollTrack.enabled = true;
                };
                _local3 = m_scrollTrack.clientRect();
                m_thumbMinPos = _local3.top;
                if (m_currentStyles.scaleScrollThumb){
                    m_scrollThumb.setStyle("boxSizing", "border-box");
                    m_scrollThumb.height = Math.ceil(((m_scrollTrack.outerHeight * pageScrollSize) / ((pageScrollSize + m_maxPos) - m_minPos)));
                    m_scrollThumb.forceRedraw();
                };
                m_thumbMaxPos = (_local3.bottom - m_scrollThumb.outerHeight);
            };
            updateScrollPosition(m_scrollPosition);
        }
        public function get lineScrollSize():Number{
            return (m_currentStyles.lineScrollSize);
        }
        override public function remove(... _args):void{
            clearInterval(m_scrollIntervalID);
            super.remove();
        }
        protected function scrollThumb_press(_arg1:MouseEvent):void{
            m_scrollIntervalID = setInterval(thumbScrub, 50, _arg1.localY);
        }
        protected function checkTargetScrollPos(... _args):void{
            if (m_textScrollOrientation == ORIENTATION_VERTICAL){
                if (((!((m_target.maxScrollV == m_maxPos))) || (!((m_target.scrollV == m_scrollPosition))))){
                    setScrollProperties((m_target.bottomScrollV - m_target.scrollV), 1, m_target.maxScrollV);
                    m_scrollPosition = m_target.scrollV;
                };
            } else {
                if (((!((m_target.maxScrollH == m_maxPos))) || (!((m_target.scrollH == m_scrollPosition))))){
                    m_scrollPosition = m_target.scrollH;
                    setScrollProperties((m_target.width - 4), 0, m_target.maxScrollH);
                };
            };
        }
        override protected function initialize():void{
            super.initialize();
            m_scrollPosition = 0;
            m_minPos = 0;
            m_maxPos = 0;
            addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel_turn);
        }
        public function get autoHide():Boolean{
            return (m_currentStyles.autoHide);
        }
        protected function scrollBtn_up(_arg1:MouseEvent):void{
            stopScroll();
        }
        protected function scrollUp_press(_arg1:MouseEvent):void{
            startScrollUp(lineScrollSize);
        }
        public function get scrollPosition():Number{
            return (m_scrollPosition);
        }
        public function get minScrollPosition():Number{
            return (m_minPos);
        }
        protected function scrollThumb_up(_arg1:MouseEvent):void{
            clearInterval(m_scrollIntervalID);
        }
        public function get maxScrollPosition():Number{
            return (m_maxPos);
        }
        override protected function createChildren():void{
            m_scrollUpBtn = SimpleButton(addChild(new SimpleButton()));
            m_scrollUpBtn.cssClasses = "scrollUpBtn";
            m_scrollTrack = SimpleButton(addChild(new SimpleButton()));
            m_scrollTrack.cssClasses = "scrollTrack";
            m_scrollDownBtn = SimpleButton(addChild(new SimpleButton()));
            m_scrollDownBtn.cssClasses = "scrollDownBtn";
            m_scrollThumb = SimpleButton(addChild(new SimpleButton()));
            m_scrollThumb.cssClasses = "scrollThumb";
            m_scrollUpBtn.addEventListener(MouseEvent.MOUSE_DOWN, scrollUp_press);
            m_scrollUpBtn.addEventListener(MouseEvent.MOUSE_UP, scrollBtn_up);
            m_scrollUpBtn.addEventListener(MouseEventConstants.MOUSE_UP_OUTSIDE, scrollBtn_up);
            m_scrollDownBtn.addEventListener(MouseEvent.MOUSE_DOWN, scrollDown_press);
            m_scrollDownBtn.addEventListener(MouseEvent.MOUSE_UP, scrollBtn_up);
            m_scrollDownBtn.addEventListener(MouseEventConstants.MOUSE_UP_OUTSIDE, scrollBtn_up);
            m_scrollThumb.addEventListener(MouseEvent.MOUSE_DOWN, scrollThumb_press);
            m_scrollThumb.addEventListener(MouseEvent.MOUSE_UP, scrollThumb_up);
            m_scrollThumb.addEventListener(MouseEventConstants.MOUSE_UP_OUTSIDE, scrollThumb_up);
            m_scrollTrack.addEventListener(MouseEvent.MOUSE_DOWN, scrollTrack_press);
            m_scrollTrack.addEventListener(MouseEvent.MOUSE_UP, scrollBtn_up);
            m_scrollTrack.addEventListener(MouseEventConstants.MOUSE_UP_OUTSIDE, scrollBtn_up);
        }
        protected function startScrollDown(_arg1:Number):void{
            startScroll(_arg1, SCROLL_DIR_DOWN);
        }
        public function set autoHide(_arg1:Boolean):void{
            m_currentStyles.autoHide = _arg1;
            m_instanceStyles.setStyle("autoHide", _arg1.toString());
        }
        public function set scrollPosition(_arg1:Number):void{
            if (m_target){
                m_target.scrollV = _arg1;
            };
            updateScrollPosition(_arg1);
        }
        protected function scrollV(_arg1:Number):void{
            if (_arg1 == pageScrollSize){
                if ((((m_scrollDirection == SCROLL_DIR_DOWN)) && ((mouseY < m_scrollThumb.bottom)))){
                    return;
                };
                if ((((m_scrollDirection == SCROLL_DIR_UP)) && ((mouseY >= m_scrollThumb.top)))){
                    return;
                };
            };
            updateScrollPosition((m_scrollPosition + (m_scrollDirection * _arg1)), true);
            dispatchEvent(new DisplayEvent(Event.CHANGE));
        }
        public function overflowScrollMode():Boolean{
            return (m_overflowScrollMode);
        }
        public function setOverflowScrollMode(_arg1:Boolean):void{
            m_overflowScrollMode = _arg1;
        }
        protected function scrollDown_press(_arg1:MouseEvent):void{
            startScrollDown(lineScrollSize);
        }
        public function set pageScrollSize(_arg1:Number):void{
            m_currentStyles.pageScrollSize = _arg1;
            m_instanceStyles.setStyle("pageScrollSize", _arg1.toString());
        }
        protected function updateScrollPosition(_arg1:Number, _arg2:Boolean=false):void{
            if (_arg1 < m_minPos){
                _arg1 = m_minPos;
            } else {
                if (_arg1 > m_maxPos){
                    _arg1 = m_maxPos;
                };
            };
            m_scrollPosition = _arg1;
            if (m_maxPos <= m_minPos){
                m_scrollThumb.top = m_thumbMinPos;
            } else {
                m_scrollThumb.top = (m_thumbMinPos + Math.round((((m_thumbMaxPos - m_thumbMinPos) * (_arg1 - m_minPos)) / (m_maxPos - m_minPos))));
            };
            if (((_arg2) && (m_target))){
                if (m_textScrollOrientation == ORIENTATION_VERTICAL){
                    m_target.scrollV = m_scrollPosition;
                } else {
                    m_target.scrollH = m_scrollPosition;
                };
            };
        }
        public function setScrollTarget(_arg1:TextField, _arg2:String):void{
            m_target = _arg1;
            m_textScrollOrientation = _arg2;
            if (m_target){
                if (_arg2 == ORIENTATION_VERTICAL){
                    pageScrollSize = (m_target.bottomScrollV - m_target.scrollV);
                } else {
                    pageScrollSize = (m_target.width - 4);
                };
                addEventListener(Event.ENTER_FRAME, checkTargetScrollPos);
                checkTargetScrollPos();
            } else {
                removeEventListener(Event.ENTER_FRAME, checkTargetScrollPos);
            };
        }
        protected function scrollTrack_press(_arg1:MouseEvent):void{
            if (mouseY >= m_scrollThumb.top){
                startScrollDown(pageScrollSize);
            } else {
                startScrollUp(pageScrollSize);
            };
        }
        protected function startScrollUp(_arg1:Number):void{
            startScroll(_arg1, SCROLL_DIR_UP);
        }
        protected function startScroll(_arg1:Number, _arg2:int):void{
            m_scrollDirection = _arg2;
            scrollV(_arg1);
            clearInterval(m_scrollIntervalID);
            m_scrollIntervalID = setInterval(ProxyFunction.create(this, callScroll), 200, _arg1);
        }
        protected function callScroll(_arg1:Number):void{
            scrollV(_arg1);
            clearInterval(m_scrollIntervalID);
            m_scrollIntervalID = setInterval(ProxyFunction.create(this, scrollV), 50, _arg1);
        }
        public function get scrollDownBtn():SimpleButton{
            return (m_scrollDownBtn);
        }
        public function set scaleScrollThumb(_arg1:Boolean):void{
            m_instanceStyles.setStyle("scaleScrollThumb", _arg1.toString());
        }
        override protected function initDefaultStyles():void{
            m_elementDefaultStyles.setStyle("lineScrollSize", "1");
            m_elementDefaultStyles.setStyle("pageScrollSize", "5");
            m_elementDefaultStyles.setStyle("width", "16px");
            m_elementDefaultStyles.setStyle("height", "50px");
            m_elementDefaultStyles.setStyle("boxSizing", "border-box");
        }
        public function delayValidation():void{
            setTimeout(forceRedraw, 1);
        }
        public function get pageScrollSize():Number{
            return (m_currentStyles.pageScrollSize);
        }
        override protected function hookIntoDisplayList():void{
            if (!m_overflowScrollMode){
                super.hookIntoDisplayList();
            };
        }
        public function setMouseWheelArea(_arg1:Number, _arg2:Number, _arg3:Number, _arg4:Number):void{
            m_mouseWheelHitAreaX = _arg1;
            m_mouseWheelHitAreaY = _arg2;
            m_mouseWheelHitAreaWidth = _arg3;
            m_mouseWheelHitAreaHeight = _arg4;
        }
        public function setScrollProperties(_arg1:Number, _arg2:Number, _arg3:Number):void{
            pageScrollSize = _arg1;
            if (_arg3 < _arg2){
                _arg3 = _arg2;
            };
            m_minPos = _arg2;
            m_maxPos = _arg3;
            invalidate();
        }
        override protected function mouseWheel_turn(_arg1:MouseEvent):void{
            var _local4:Number;
            var _local2:Number = parent.mouseX;
            var _local3:Number = parent.mouseY;
            if (((((((m_target) && ((((_local2 >= x)) && ((_local2 <= (x + outerWidth))))))) && ((((_local3 >= y)) && ((_local3 <= (y + outerHeight))))))) || ((((((_local2 >= m_mouseWheelHitAreaX)) && ((_local2 <= (m_mouseWheelHitAreaX + m_mouseWheelHitAreaWidth))))) && ((((_local3 >= m_mouseWheelHitAreaY)) && ((_local3 <= (m_mouseWheelHitAreaY + m_mouseWheelHitAreaHeight))))))))){
                _local4 = (scrollPosition - (lineScrollSize * _arg1.delta));
                updateScrollPosition(_local4, true);
                dispatchEvent(new DisplayEvent(Event.CHANGE));
            };
        }
        override protected function applyStyles():void{
            super.applyStyles();
            switch (m_currentStyles.buttonPositioning){
                case "top":
                    m_children[0] = m_scrollUpBtn;
                    m_children[1] = m_scrollDownBtn;
                    m_children[2] = m_scrollTrack;
                    break;
                case "bottom":
                    m_children[0] = m_scrollTrack;
                    m_children[1] = m_scrollUpBtn;
                    m_children[2] = m_scrollDownBtn;
                    break;
                case "separated":
                default:
                    m_children[0] = m_scrollUpBtn;
                    m_children[1] = m_scrollTrack;
                    m_children[2] = m_scrollDownBtn;
            };
        }
        public function get scaleScrollThumb():Boolean{
            return (Boolean(m_currentStyles.scaleScrollThumb));
        }
        protected function thumbScrub(_arg1:Number):void{
            var _local2:Number = (mouseY - _arg1);
            if (_local2 < m_thumbMinPos){
                _local2 = m_thumbMinPos;
            } else {
                if (_local2 > m_thumbMaxPos){
                    _local2 = m_thumbMaxPos;
                };
            };
            var _local3:Number = m_scrollPosition;
            updateScrollPosition(Math.round((((_local2 - m_thumbMinPos) * m_maxPos) / (m_thumbMaxPos - m_thumbMinPos))), true);
            if (m_scrollPosition != _local3){
                dispatchEvent(new DisplayEvent(Event.CHANGE));
            };
        }
        protected function stopScroll():void{
            clearInterval(m_scrollIntervalID);
        }

    }
}//package reprise.controls 
